/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved.
 */
package com.kaaction.modules.operate.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.operate.entity.ActivityChannel;
import com.kaaction.modules.operate.service.ActivityChannelService;

/**
 * 活动/渠道关联Controller
 * @author zengbiao
 * @version 2017-04-12
 */
@Controller
@RequestMapping(value = "${adminPath}/operate/activityChannel")
public class ActivityChannelController extends BaseController {

	@Autowired
	private ActivityChannelService activityChannelService;
	
	@ModelAttribute
	public ActivityChannel get(@RequestParam(required=false) String id) {
		ActivityChannel entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = activityChannelService.get(id);
		}
		if (entity == null){
			entity = new ActivityChannel();
		}
		return entity;
	}
	
	@RequiresPermissions("operate:activityChannel:view")
	@RequestMapping(value = {"list", ""})
	public String list(ActivityChannel activityChannel, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ActivityChannel> page = activityChannelService.findPage(new Page<ActivityChannel>(request, response), activityChannel); 
		model.addAttribute("page", page);
		return "modules/operate/activityChannelList";
	}

	@RequiresPermissions("operate:activityChannel:view")
	@RequestMapping(value = "form")
	public String form(ActivityChannel activityChannel, Model model) {
		model.addAttribute("activityChannel", activityChannel);
		return "modules/operate/activityChannelForm";
	}

	@RequiresPermissions("operate:activityChannel:edit")
	@RequestMapping(value = "save")
	public String save(ActivityChannel activityChannel, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, activityChannel)){
			return form(activityChannel, model);
		}
		activityChannelService.save(activityChannel);
		addMessage(redirectAttributes, "保存活动/渠道成功");
		return "redirect:"+Global.getAdminPath()+"/operate/activityChannel/?repage";
	}
	
	@RequiresPermissions("operate:activityChannel:edit")
	@RequestMapping(value = "delete")
	public String delete(ActivityChannel activityChannel, RedirectAttributes redirectAttributes) {
		activityChannelService.delete(activityChannel);
		addMessage(redirectAttributes, "删除活动/渠道成功");
		return "redirect:"+Global.getAdminPath()+"/operate/activityChannel/?repage";
	}

}